package com.qk.ezbuy.controller;

import com.github.pagehelper.PageInfo;
import com.qk.ezbuy.pojo.News;
import com.qk.ezbuy.pojo.ProductCategory;
import com.qk.ezbuy.service.ProductCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/ProductCategory")
public class ProductCategoryController {
    @Autowired
    private ProductCategoryService productCategoryService;
    //查询所有分类
    @RequestMapping("/selectAll")
    public PageInfo<ProductCategory> selectByPage(@RequestParam int pageNum, @RequestParam int pageSize){
        PageInfo<ProductCategory> pageInfo=null;
        pageInfo=productCategoryService.selectAll(pageNum,pageSize);
        return pageInfo;
    }
    //删除分类
    @RequestMapping("/delProductCategoryById")
    public Map delProductCategoryById(int id){
        System.out.println("=================");
        List<ProductCategory> productCategory = productCategoryService.selectProductCategoryAll();
        Map<String,Object> map = new HashMap<>();
        boolean flas = false;
        int count=0;
        for(ProductCategory productCategory1:productCategory){
            if (productCategory1.getParentid()==id){
                flas = true;
            }
        }
        if(!flas){
            count = productCategoryService.delProductCategoryById(id);
        }

        if(count>0){
            map.put("msg",1);
        }else {
            map.put("msg",2);
        }
        return map;
    }
    /**
     * 增加
     */
    @RequestMapping("addProductCategoryService")
    public Map addProductCategoryService(ProductCategory productCategory){
        Map<String,Object> map = new HashMap<>();
        System.out.println(productCategory.getName()+"===================");
        int count = productCategoryService.insertSelective(productCategory);
        if(count>0){
            map.put("msg",1);
        }else {
            map.put("msg",2);
        }
        return map;
    }

    /**
     * 查询分类
     */
    @RequestMapping("selectProductCategoryAll")
    public List selectProductCategoryAll (){
        List<ProductCategory> list = productCategoryService.selectProductCategoryAll();
        return list;
    }

    /**
     * 查询分类
     */
    @RequestMapping("selectProductCategoryByType")
    public List selectProductCategoryByType (int type){
        List<ProductCategory> list = productCategoryService.selectProductCategoryByType(type);
        return list;
    }

    /**
     * 修改分类
     */
    @RequestMapping("updateByPrimaryKeySelective")
    public Map updateByPrimaryKeySelective (ProductCategory productCategory){
        Map<String,Object> map = new HashMap<>();
        int count = productCategoryService.updateByPrimaryKeySelective(productCategory);
        System.out.println(count+"=========");
        if(count>0){
            map.put("msg",1);
        }else {
            map.put("msg",2);
        }
        return map;
    }

    @RequestMapping("selectProductCategoryById")
    public ProductCategory selectProductCategoryById (int id){
        Map<String,Object> map = new HashMap<>();
        ProductCategory productCategory = productCategoryService.selectProductCategoryById(id);
        return productCategory;
    }
}
